package com.engineering.elink.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import springfox.documentation.annotations.ApiIgnore;

import com.engineering.elink.co.BaseCo;
import com.engineering.elink.co.VideoCommentCo;
import com.engineering.elink.service.VideoCommentService;
import com.engineering.elink.xaservice.EvaluateXaService;

@RestController
@CrossOrigin
@RequestMapping("/api/videocomment")
@Api(value = "视频评论接口")
@ApiIgnore
public class VideoCommentController {
	
	@Autowired
	private VideoCommentService videoCommentService;
	
	@Autowired
	private EvaluateXaService evaluateXaService;

	/**
	 * 分页获取视频的一级评论
	 */
	@RequestMapping(value = "/getTopCommentsByVideoId", method = RequestMethod.GET)
	@ApiOperation(value = "分页获取视频的一级评论")
	public Map<String, Object> getTopCommentsByVideoId(@ModelAttribute @Valid VideoCommentCo co) {
		return videoCommentService.getTopCommentsByVideoId(co);
	}
	
	/**
	 * 通过层级Id,分页获取一级评论下的所有回复评论

	 */
	@RequestMapping(value = "/getAllCommentsByTopId", method = RequestMethod.GET)
	@ApiOperation(value = "分页获取一级评论下的所有回复评论")
	public Map<String, Object> getAllCommentsByTopId(@ModelAttribute @Valid BaseCo co,
			@ApiParam(value="一级评论的id",required=true) @RequestParam("topId")Integer topId) {
		
		return videoCommentService.getAllCommentsByTopId(co, topId,true);
	}
	
	@RequestMapping(value = "/getAllByTopId", method = RequestMethod.GET)
	@ApiOperation(value = "分页获取一级评论下的所有回复评论包括当前评论")
	public Map<String, Object> getAllByTopId(@ModelAttribute @Valid BaseCo co,
			@ApiParam(value="一级评论的id",required=true) @RequestParam("topId")Integer topId) {
		
		return videoCommentService.getAllCommentsByTopId(co,topId,false);
	}
	
	/**
	 * 发表评论
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ApiOperation(value = "发表评论")
	public Map<String, Object> add(
			@ApiParam(value = "视频id", required = true) @RequestParam("id") Integer id,
			@ApiParam(value = "评论内容", required = true) @RequestParam("content") String content			
			,HttpServletRequest request) {
		@SuppressWarnings("unchecked")
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		String userId =  userInfo.get("userId").toString();
		
		return evaluateXaService.addVideo(id, userId, content);
	}
	
	/**
	 * 回复评论
	 */
	@RequestMapping(value = "/addReply", method = RequestMethod.POST)
	@ApiOperation(value = "回复评论")
	public Map<String, Object> addReply(
			@ApiParam(value = "视频id", required = true) @RequestParam("id") Integer id,
			@ApiParam(value = "评论id", required = true) @RequestParam("commentId") Integer commentId,
			@ApiParam(value = "评论内容", required = true) @RequestParam("content") String content,HttpServletRequest request) {
		@SuppressWarnings("unchecked")
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		String userId =  userInfo.get("userId").toString();
		
		return evaluateXaService.addReply(id, commentId, userId, content);
	}

	/**
	 * 评论点赞(顶/踩)
	 */
	@RequestMapping(value = "addPraise", method = RequestMethod.POST)
	@ApiOperation(value = "评论点赞(顶/踩)")
	public Map<String, Object> addPraise(
			@ApiParam(value = "评论id", required = true) @RequestParam("commentId") Integer commentId,
			@ApiParam(value = "点赞类别，1-顶，2-踩", required = true,defaultValue="1") @RequestParam("praiseType") Integer praiseType,HttpServletRequest request) {
		@SuppressWarnings("unchecked")
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		String userId =  userInfo.get("userId").toString();
		
		return videoCommentService.addPraise(commentId, praiseType, userId);
	}
	
	/**
	 * 获取评论我的视频信息
	 */
	@RequestMapping(value="/commentForMe",method=RequestMethod.GET)
	@ApiOperation(value="获取评论我的")
	public Map<String, Object> commentForMe(@ModelAttribute @Valid BaseCo co,HttpServletRequest request){
		@SuppressWarnings("unchecked")
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		String userId =  userInfo.get("userId").toString();
		
		return videoCommentService.getCommentForMe(co,userId);
	}
	
	/**
	 *  获取我评论的视频信息
	 */
	@RequestMapping(value="/myComment",method=RequestMethod.GET)
	@ApiOperation(value="获取我评论的")
	public 	Map<String, Object>myComment(@ModelAttribute @Valid BaseCo co,HttpServletRequest request){
		@SuppressWarnings("unchecked")
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		String userId =  userInfo.get("userId").toString();
		
		return videoCommentService.getMyComment(co,userId);
	}
}
